/*
 * Copyright 2000 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _WT_DB_SESSION_H
#define _WT_DB_SESSION_H

#include <tcl.h>
#include "WtSession.h"

typedef struct WtDbSessionT
{
  /* Session ID */

  Tcl_Obj *id;
  int isAutoId;

  /* Updates */

  int isOpen;
  int isFetched;
  Tcl_Obj *items;
  Tcl_Obj *checkpoint;
  int isModified;
  Tcl_Obj *deletions;
  Tcl_Obj *insertions;
  int isCleared;
  int commitStatus;
  time_t accessTime;
  int syncedAccessTime;
  int accessTimeInterval;

  /* DB state */

  Tcl_Obj *dbHandler;
  Tcl_Obj *dbHandlerData;
  int dbHandlerLoaded;
  int dbSessionOpened;
} WtDbSession;

Tcl_Obj *WtNewDbSessionObj();
WtDbSession *WtDbSessionRep(Tcl_Obj *obj);
int WtInitDbSessionId(WtDbSession *ds, Tcl_Interp *interp);
Tcl_Obj *WtGetDbSessionId(WtDbSession *ds, Tcl_Interp *interp);
int WtSetDbSessionId(WtDbSession *ds, Tcl_Obj *id, Tcl_Interp *interp);
int WtOpenDbSession(WtDbSession *ds, Tcl_Interp *interp);
int WtCloseDbSession(WtDbSession *ds, Tcl_Interp *interp, int doCommit);
int WtOpenDbSessionResource(WtDbSession *ds, Tcl_Interp *interp);
int WtCloseDbSessionResource(WtDbSession *ds, Tcl_Interp *interp);
int WtSetDbSessionHandler(WtDbSession *ds, Tcl_Obj *handler, Tcl_Interp *interp);
int WtLoadDbSessionStore(WtDbSession *ds, Tcl_Interp *interp);
int WtUnloadDbSessionStore(WtDbSession *ds, Tcl_Interp *interp, int doClose);
int WtFetchDbSession(WtDbSession *ds, Tcl_Interp *interp);
Tcl_Obj *WtGetDbSessionItems(WtDbSession *ds, Tcl_Interp *interp);
Tcl_Obj *WtGetDbSessionInsertions(WtDbSession *ds, Tcl_Interp *interp);
Tcl_Obj *WtGetDbSessionDeletions(WtDbSession *ds, Tcl_Interp *interp);
int WtCommitIsPending(WtDbSession *ds);
int WtCommitDbSession(WtDbSession *ds, Tcl_Interp *interp);
int WtDeleteDbSession(WtDbSession *ds, Tcl_Obj *id, Tcl_Interp *interp);
int WtSweepDbSessions(WtDbSession *ds, Tcl_Interp *interp);
int WtClearDbSessionCache(WtDbSession *ds, Tcl_Interp *interp);
int WtClearDbSessionUpdates(WtDbSession *ds, Tcl_Interp *interp);
int WtUnloadDbSession(WtDbSession *ds, Tcl_Interp *interp);
int WtDbSessionFinalizeRequest(WtDbSession *ds, Tcl_Interp *interp);
int WtDbSessionCmd(ClientData clientData, Tcl_Interp *interp,
  int objc, Tcl_Obj *const objv[]);
int WtDbSessionCmdHelper(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
WtDbSessionCmdUsage(Tcl_Interp *interp, Tcl_Obj *cmd);
int WtDbSessionIdCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionCountCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionHasCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionGetCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionGetAllCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionSetCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionSetDefaultCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionSetAllCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionSetAllDefaultCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionAddCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionAddListCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionKeysCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionValuesCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionAllValuesCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionRemoveCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionRemoveListCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionArrayCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionListArrayCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionItemsCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionClearCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionIsAccessedCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionIsModifiedCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionCommitCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionTouchCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionClearCacheCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionStatusCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionPropertiesCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionDeleteCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionSweepCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionHandlerCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionHandlerDataCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionHandlerEvalCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionUnloadCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionLoadModuleCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionUnloadModuleCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionFinalizeCmd(ClientData clientData, Tcl_Interp *interp,
  WtDbSession *ds, int objc, Tcl_Obj *const objv[]);
int WtDbSessionHandler(WtDbSession *ds, Tcl_Obj *handler,
  Tcl_Obj *method, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
int WtDbSessionHandlerStr(WtDbSession *ds, Tcl_Obj *handler,
  const char *methodStr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
int WtDbSessionHandlerInternal(WtDbSession *ds,
  Tcl_Obj *handler, Tcl_Obj *method, Tcl_Interp *interp,
  int objc, Tcl_Obj *const objv[]);
int WtPrepareDbSessionHandler(WtDbSession *ds, Tcl_Obj *method, Tcl_Interp *interp);
void WtInitDbSessionCommands(Tcl_Interp *interp);

extern struct Tcl_ObjType WtDbSessionType;

#endif /* !_WT_DB_SESSION_H */
